/**
 *
 *  首场景 
 *
 */
class MainScreen extends BaseScreen {
    public constructor() {
        super();
        this.skinName = "resource/eui_skins/MainScreenSkin.exml";

    }

    public myscrollView: ScrollView;

    public rect_snow: eui.Rect;
    public group_snow: eui.Group;
    private create_time: number;
    public bm_scroller: eui.Scroller;
    private _is_snow: boolean = false;
    private rect_sbg:eui.Rect;
    private page_num:number = 16;
    private img_jiantou:eui.Image;
    private img_jiantou1:eui.Image;
    protected childrenCreated(): void {
        super.childrenCreated();
        this.init_view();
        this.myscrollView.set_main_screen(this);
        this.myscrollView.itemNum = 16;
        this.myscrollView.bounces = false;

        this.myscrollView.addEventListener(egret.Event.COMPLETE, this.onComplete, this);
        this.myscrollView.addEventListener(egret.Event.CHANGE, this.onChange, this);
        // this.myscrollView.addEventListener(egret.Event.CHANGE,this.onEnd,this);
        this.addEventListener(egret.Event.ENTER_FRAME,this.process,this);
        this.create_time = 0;
    }

    private onComplete(event: egret.Event) {
        console.log("on Complete")
    }

    private onChange(event: egret.Event) {
        console.log("on Change");
    }

    public scroll_end(item: number) {

        console.log('scroll_end_item=' + item);
        for (var i = 1; i <= this.page_num; i++) {
            if (this['page_' + i]) {
                this['page_' + i].out_page();
            }
        }

        if (this['page_' + (item + 1)]){
            this['page_' + (item + 1)].showPage();
        }

        if(item == 0){
            this.img_jiantou.visible = false;
            this.img_jiantou1.visible = true;
        }
        else if(item == 15){
            this.img_jiantou.visible = false;
            this.img_jiantou1.visible = false;
        }
        else{
            this.img_jiantou.visible = true;
            this.img_jiantou1.visible = false;
        }
    }


    public init_view() {
         egret.Tween.get(this.img_jiantou,{loop:true}).to({alpha:0.2},1500).to({alpha:1},1500);
         egret.Tween.get(this.img_jiantou1,{loop:true}).to({alpha:0.2},1500).to({alpha:1},1500);
    }

    private initDb() {

    }

    public resize(): void {
        super.resize();
        
        if (this.height > 1138) {
            this.myscrollView.height = this.height;
            this.myscrollView.itemSize = this.height;
        }

        for (var i = 1; i <= this.page_num; i++) {
            if (this['page_' + i]) {
                if (this.height > 1138) {
                    this['page_' + i].y = (i - 1) * this.height;
                    this['page_' + i].height = this.height;
                } else {
                    this['page_' + i].y = (i - 1) * 1138;
                    this['page_' + i].height = 1138;
                }
            }

        }

         this.img_jiantou.y = this.height - this.img_jiantou.width - 30;
         this.img_jiantou1.y = this.height - this.img_jiantou1.width - 30;
    }

    public show_bm(param) {
        this.bm_scroller.alpha = 0;
        this.bm_scroller.visible = true;
        egret.Tween.get(this.bm_scroller).to({ alpha: 1 }, 300);
    }

    public close_bm() {
        let self = this;
        egret.Tween.get(this.bm_scroller).to({ alpha: 0 }, 300).call(function () {
            self.bm_scroller.visible = false;
        });
    }

    public process(){
        for (var i = 1; i <= this.page_num; i++) {
            if (this['page_' + i]) {
                this['page_' + i].process();
            }
        }
    }
}

